//
//  YYcollectionViewController.m
//  endLess_photos
//
//  Created by mac on 15/6/19.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "YYcollectionViewController.h"
#import "YYcollectionViewCell.h"
@interface YYcollectionViewController ()
//--/当前显示图片的索引/-----
@property (nonatomic, assign) NSInteger currentIndex;
//--/布局对象/-----
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
//--/图片数组/-----
@property (nonatomic, strong) NSArray *imageList;
@end
@implementation YYcollectionViewController
///MARK:----/图片数组懒加载/--------
-(NSArray *)imageList
{
    if(_imageList == nil)
    {
        NSMutableArray *Marray = [NSMutableArray array];
        for(int i = 0; i<3; i++)
        {
            NSString *imageName = [NSString stringWithFormat:@"%02d.png",i];
            [Marray addObject:imageName];
        }
        _imageList = Marray.copy;
    }
    return _imageList;
    
}
///MARK:----/cell重用标示符/--------
static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    
    [super viewDidLoad];

}
///MARK:----/设置布局对象/--------
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _flowLayout.itemSize = self.collectionView.bounds.size;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    //--/设置图像的索引/-----
    _currentIndex = 0;
    NSIndexPath *indexpath = [NSIndexPath indexPathForItem:1 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    
}

///MARK:----/数据源方法/--------
#pragma mark UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    YYcollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    //--/根据当前的图像索引来显示图像/-----
    NSInteger index = (indexPath.item - 1 + self.imageList.count + self.currentIndex) % self.imageList.count;
    [cell SetImageWithName:self.imageList[index]];
    return cell;
}
#pragma mark UICollectionViewDelegate
///MARK:----/实现无限左右滚动/--------
//--/此代理方法在每一帧图片结束滚动调用/-----
-(void)scrollViewDidEndDecelerating:(nonnull UIScrollView *)scrollView
{
    //--/x方向偏移量/-----
    NSInteger offsetX = scrollView.contentOffset.x;
    //--/collectionView窗口宽度/-----
    NSInteger viewW = scrollView.bounds.size.width;
    //--/计算是否要切换图片/-----
    NSInteger offset = offsetX/viewW - 1;
    if (offset != 0)
    {
        _currentIndex = (_currentIndex + offset + self.imageList.count) % self.imageList.count;
        NSIndexPath *indexpath = [NSIndexPath indexPathForItem:1 inSection:0];
        [self.collectionView scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
        
        //--/解决快速拖拽跳帧的现象/-----
        //--/1 关闭动画/-----
        [UIView setAnimationsEnabled:NO];
        //--/2 刷新第一页的Cell  注意@[indexpath]表示的是一个数组（只有一个元素）/-----
        [self.collectionView reloadItemsAtIndexPaths:@[indexpath]];
        //--/3 打开动画/-----
        [UIView setAnimationsEnabled:YES];
        
    }
}

@end
